'use client';

import '@/app/styles/data-table.scss';
import DataTable, { DataTableColumn } from '@/components/table/DataTable';
import { StockDividendRow, DIVIDEND_FIX_LABEL } from '@/types/seibro';
import { codeLabel, formatDecimal, formatRatePercent, dateOrDash } from '@/lib/utils/market';

function buildColumns(): DataTableColumn<StockDividendRow>[] {
	return [
		{
			key: 'rgtStdDt',
			header: '권리기준일',
			align: 'left',
			priority: 'high',
			cell: (row) => row.rgtStdDt
		},
		{
			key: 'xrgtDt',
			header: '권리락일',
			align: 'center',
			priority: 'medium',
			cell: (row) => dateOrDash(row.xrgtDt)
		},
		{
			key: 'cashPerShare',
			header: '주당배당금',
			align: 'right',
			priority: 'high',
			cell: (row) => (row.cashPerShare === null ? '—' : `${formatDecimal(row.cashPerShare, 0)}원`)
		},
		{
			key: 'marketDividendRate',
			header: '시가배당율',
			align: 'right',
			priority: 'high',
			cell: (row) => formatRatePercent(row.marketDividendRate)
		},
		{
			key: 'stockAllocRatio',
			header: '주식배정비율',
			align: 'right',
			priority: 'low',
			cell: (row) => (row.stockAllocRatio === null ? '—' : formatDecimal(row.stockAllocRatio, 4))
		},
		{
			key: 'cashPayDate',
			header: '지급일',
			align: 'center',
			priority: 'low',
			cell: (row) => dateOrDash(row.cashPayDate)
		},
		{
			key: 'fixType',
			header: '확정',
			align: 'center',
			priority: 'low',
			cell: (row) => codeLabel(row.fixType, DIVIDEND_FIX_LABEL)
		}
	];
}

export default function StockDividends({ rows }: { rows: StockDividendRow[] })
{
	return (
		<DataTable<StockDividendRow>
			caption='배당 이력 — 권리기준일, 권리락일, 주당배당금, 시가배당율, 주식배정비율, 지급일, 확정'
			columns={buildColumns()}
			rows={rows}
			rowKey={(row, index) => `${row.rgtStdDt}-${index}`}
			emptyMessage='수집된 배당 이력이 없습니다.'
		/>
	);
}
